<style>
    .layui-form-item .layui-input-company {
        width: auto;
        padding-right: 10px;
        line-height: 38px;
    }
</style>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">旧的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="oldPassword" lay-verify="required" lay-reqtext="旧的密码不能为空" placeholder="请输入旧的密码" class="layui-input" autocomplete="new-password">
                    <tip>填写自己账号的旧的密码。</tip>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="newPassword" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请输入新的密码" class="layui-input" autocomplete="new-password">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="againPassword" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请再次输入新的密码" class="layui-input" readonly onfocus="this.removeAttribute('readonly');" onblur="this.setAttribute('readonly',true);">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'miniPage', 'miniAjax'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniAjax = layui.miniAjax,
            miniPage = layui.miniPage;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var newPassword = data.field.newPassword;
            var againPassword = data.field.againPassword;
            if (newPassword != againPassword) {
                layer.msg('新密码前后输入不一致，请检查！', {time: 1000, icon: 5});
                return false;
            }
            data.field.oldPassword = md5.MD5(data.field.oldPassword);
            data.field.newPassword = md5.MD5(data.field.newPassword);
            data.field.againPassword = md5.MD5(data.field.againPassword);

            layer.confirm('确定要修改密码吗', {
                title: '提示'
            }, function (index) {
                miniAjax.postJSON({
                    url: '/auth/editPassword',
                    data: JSON.stringify(data.field),
                    success: function (res) {
                        if (res.code == '0') {
                            layer.msg(res.msg, {time: 1000, icon: 1}, function () {
                                layer.close(index);
                                window.location = 'login.html';
                            });
                        } else {
                            layer.msg(res.msg, {time: 1000, icon: 2});
                        }
                    }
                });
            });

            return false;
        });

    });
</script>